<?php
/**
 * Copyright (c) Enalean, 2017. All Rights Reserved.
 *
 * This file is a part of Tuleap.
 *
 * Tuleap is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Tuleap is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tuleap. If not, see <http://www.gnu.org/licenses/>.
 */

namespace Tuleap\User\History;

use Event;

class HistoryRetriever
{
    const MAX_LENGTH_HISTORY = 30;

    /**
     * @var \EventManager
     */
    private $event_manager;

    public function __construct(\EventManager $event_manager)
    {
        $this->event_manager = $event_manager;
    }

    /**
     * @return HistoryEntry[]
     */
    public function getHistory(\PFUser $user)
    {
        $history = array();

        $this->event_manager->processEvent(
            Event::USER_HISTORY,
            array(
                'user'    => $user,
                'history' => &$history
            )
        );

        $this->sortHistoryByVisitTime($history);

        return array_slice($history, 0, self::MAX_LENGTH_HISTORY);
    }

    private function sortHistoryByVisitTime(array &$history)
    {
        usort($history, function (HistoryEntry $a, HistoryEntry $b) {
            if ($a->getVisitTime() === $b->getVisitTime()) {
                return 0;
            }
            return $a->getVisitTime() > $b->getVisitTime() ? -1 : 1;
        });
    }
}
